<template>
	<view style="padding: 10px;">
		<u-form :model="formData" ref="uForm">
			<u-form-item label="车主姓名"  :border-bottom="false"  label-width="150" prop="czxm">
				<u-input v-model="formData.czxm" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="车辆型号"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.clxh" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="车牌号"  :border-bottom="false"  label-width="150" prop="cph">
				<u-input v-model="formData.cph"  border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="车架号"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.cjh" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="发动机号"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.fdjh" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="颜色"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.ys" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="手机号"  :border-bottom="false"  label-width="150" >
				<u-input v-model="formData.czsjh" border="true" :disabled="disabled"/>
			</u-form-item>
			<u-form-item label="防盗装置"  border-bottom="false"  label-width="150" >
				<u-radio-group v-model="formData.fdzz" :disabled="disabled">
					<u-radio 
						@change="radioChange" 
						v-for="(item, index) in results" :key="index" 
						:name="item.value"
					>
						{{item.label}}
					</u-radio>
				</u-radio-group>
			</u-form-item>
		</u-form>
		<u-button v-if="!disabled" @click="save(1,false)" :disabled="submitBtn" type="primary">保存</u-button>
		<view class="buttons" v-if="hasChecked">
			<u-button @click="checked()" :disabled="submitBtn" type="primary">审核通过</u-button>
			<u-button @click="unChecked()" :disabled="submitBtn" type="success">审核不通过</u-button>
		</view>
		<u-modal show-cancel-button="true"  @confirm="confirm" v-model="unCheckedModalShow" :title="content" >
			<view style="padding: 10px;">
				<u-input v-model="formData.unCheckedReason" border="true" type="textarea" :auto-height="true" />
			</view>
			
		</u-modal>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import { mapState} from 'vuex';
	export default{
		computed:mapState(['user', ]),
		data(){
			return {
				submitBtn:false,
				unCheckedModalShow:false,
				content:'请输入原因',
				disabled:false,
				hasChecked:false,
				formData:{
					czxm:'',
					clxh:'',
					cph:'',
					cjh:'',
					fdjh:'',
					ys:'',
					czsjh:'',
					fdzz:'1',
					state:'1',
					tjr:'',
					unCheckedReason:''
				},
				results: [
					{
						value: '1',
						label: '安装'
					},
					{
						value: '0',
						label: '未安装'
					}
				],
				rules: {
					czxm: [
						{ 
							required: true, 
							message: '车主姓名不能为空',  
							trigger: ['change','blur'],
						}
					],
					cph: [
						{ 
							required: true, 
							message: '车牌号不能为空',  
							trigger: ['change','blur'],
						}
					]
				}
			}
		},
		methods:{
			doSave(){
				const that = this;
				this.submitBtn = true;
				this.$http.post("/xxcj/cldj/save",this.formData).then(res =>{
					let data = res.data.data;
					if(data){
						that.$refs.uToast.show({
							title: '保存成功',
							type: 'success',
							callback: ()=>{
								let pages = getCurrentPages(); // 当前页面
								console.log(pages);
								let prePage = pages[pages.length - 2]; // 前一个页面
								uni.navigateBack({
								     success: function() {
										prePage.onLoad(); // 执行前一个页面的onLoad方法
								     }
								});
							}
						});
					}else{
						that.submitBtn = false;
					}
				}).catch(err => {
					that.submitBtn = false;
					that.$refs.uToast.show({
						title: '保存失败请稍后重试...',
						type: 'error',
					})
				})
			},
			save(state,isCheck){
				const that = this;
				if(state)
					this.formData.state = state;
				if(isCheck){
					this.doSave();
				}else{	
					this.formData.tjr = this.user.user.id;
					this.$refs.uForm.validate(valid => {
						if (valid) {
							that.doSave();
						} else {
							this.submitBtn = false;
							console.log('表单信息不完整');
						}
					});
				}
			},
			radioChange(e) {
				this.formData.fdzz = e;
			},
			//审核通过
			checked(){
				this.save(2,true);
			},
			//审核不通过
			unChecked(){
				this.unCheckedModalShow = true;
			},
			confirm(){
				this.save(3,true);
			},
			checkHasPermission(){
				if(this.formData.id){
					//没有修改权限，或者已审核通过，或者当点内容非本用户，设置内容为禁用状态
					if(this.user.user.auths.indexOf('edit')<0||this.formData.state==='2'|| this.formData.tjr != this.user.user.id){
						this.disabled = true;
					}
					//有审核权限，并且未审核状态，并且非本用户内容
					if(this.user.user.auths.indexOf('check')>-1&&this.formData.state==='1'&&this.formData.tjr != this.user.user.id){
						this.hasChecked = true;
					}
				}
			}
		},
		onLoad(options) {
			Object.assign(this.formData,options.item?JSON.parse(options.item):{});
			this.$refs.uForm.setRules(this.rules);
			this.checkHasPermission();
		}
	}
</script>

<style scoped>
	.buttons{
		display: flex;
		width: 100%;
		margin-top: 20upx;
		justify-content: center;
	}
	.buttons u-button{
		margin: 0 50rpx;
	}
</style>